package com.syzsn.common.controller;

import com.syzsn.common.enums.ContinentSubType;
import com.syzsn.common.enums.ContinentType;
import com.syzsn.common.enums.SK2013Type;
import com.syzsn.common.entity.SyCountry;
import com.syzsn.common.repository.SyCountryRepository;
import com.syzsn.sk8.enums.SK8Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/country")
public class SyCountryController {

    @Autowired
    private SyCountryRepository syCountryRepository;
    @GetMapping("/list")
    public ModelAndView list(ModelMap model
            , @RequestParam(name = "continentType", required = false) ContinentType continentType
            , @RequestParam(name = "continentSubType", required = false) ContinentSubType continentSubType
    ){
        List<SyCountry> syCountryList;
        if(continentType!=null && continentSubType != null){
            syCountryList = syCountryRepository.findCountryListByType(continentType.name(), continentSubType.name());
        }else if(continentType!=null) {
            syCountryList = syCountryRepository.findCountryListByContinentType(continentType.name());
        }else if(continentSubType != null){
            syCountryList = syCountryRepository.findCountryListByContinentSubType(continentSubType.name());
        }else{
            model.put("msg", "全部国家和地区");
            syCountryList = syCountryRepository.findAll();
        }
        model.put("syCountryList", syCountryList);
        model.put("continentType", continentType);
        model.put("continentSubType", continentSubType);
        return new ModelAndView("country_list");
    }

    @GetMapping("/sk8_list")
    public ModelAndView sk8list(ModelMap model, @RequestParam(name = "sk8type", required = false) SK8Type sk8Type){
        List<SyCountry> syCountryList;
        if(sk8Type!=null){
            syCountryList = syCountryRepository.findSK8CountryListBySk8Type(sk8Type.name());
        }else{
            model.put("msg", "全部国家和地区");
            syCountryList = syCountryRepository.findSK8CountryList();
        }
        model.put("syCountryList", syCountryList);
        model.put("sk8Type", sk8Type);
        return new ModelAndView("sk8_list");
    }

    @GetMapping("/sk2013_list")
    public ModelAndView sk2013list(ModelMap model, @RequestParam(name = "sk2013type", required = false) SK2013Type sk2013Type){
        List<SyCountry> syCountryList;
        if(sk2013Type!=null){
            syCountryList = syCountryRepository.findSK2013CountryListBySk2013Type(sk2013Type.name());
        }else{
            model.put("msg", "全部国家和地区");
            syCountryList = syCountryRepository.findSK2013CountryList();
        }
        model.put("syCountryList", syCountryList);
        model.put("sk2013Type", sk2013Type);
        return new ModelAndView("sk2013_list");
    }
}
